<template>
  <div class="app-container">
    <el-form v-show="state.showSearch" :model="queryParams" ref="queryRef" :inline="true">
      <el-form-item label="广告名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="广告类型" prop="type">
        <el-select v-model="queryParams.type" clearable class="m-2" placeholder="请选择">
          <el-option
            v-for="item in adTypeOptions"
            :key="item"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="&nbsp;">
        <el-button @click="handleSearch(false)">重置</el-button>
        <el-button type="primary" @click="handleSearch(true)">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['advert:info:add']"
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar v-model:showSearch="state.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="state.loading" :data="tableList">
      <el-table-column
        label="广告名称"
        align="center"
        prop="name"
        min-width="120"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="广告类型"
        align="center"
        prop="type"
        min-width="90"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span> {{ scope.row.type === 1 ? '图文' : '视频' }} </span>
        </template>
      </el-table-column>
      <el-table-column label="广告内容" align="center" prop="resourceUrl" min-width="120">
        <template #default="scope">
          <div v-if="scope.row.type == 1">
            <el-image
              :src="scope.row.resourceUrl"
              preview-teleported
              hide-on-click-modal
              style="height: 100px"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="[scope.row.resourceUrl]"
              :initial-index="0"
              fit="cover"
            />
          </div>
          <!-- <el-dialog v-model="dialogVisible" title="预览" width="800px" append-to-body>
            <img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto" />
          </el-dialog> -->
          <div v-if="scope.row.type == 2">
            <video
              :src="scope.row.resourceUrl"
              width="200"
              preload
              :autoplay="false"
              controls
              class="video-box"
            ></video>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="时长"
        align="center"
        prop="duration"
        min-width="90"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span v-if="scope.row.type === 2"
            >{{ scope.row.duration ? scope.row.duration : '0' }} 秒</span
          >
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="90"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作" align="center" fixed="right" min-width="140">
        <template #default="scope">
          <el-button
            v-hasPermi="['advert:info:edit']"
            link
            type="primary"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            v-hasPermi="['advert:info:delete']"
            link
            type="primary"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="state.total > 0"
      :total="state.total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList()"
    />

    <operate
      ref="operateRef"
      :operType="state.operType"
      :adTypeOptions="adTypeOptions"
      @updateList="getList()"
    />
  </div>
</template>

<script setup name="advertmanage">
import { ElMessage, ElMessageBox } from 'element-plus'
import { getAdvertList, deleteAdvert } from '@/api/advert'
import { reactive } from 'vue'
import operate from './templates/operate.vue'

const queryRef = ref(null)
const { proxy } = getCurrentInstance()

// resourceType 2 视频  resourceType 1 imgType 1封面  resourceType 1 imgType 2详情图
const adTypeOptions = ref([
  { name: '图文', value: 1 },
  { name: '视频', value: 2 }
])
const AreaOptions = ref([])
const state = reactive({
  currentId: null,
  actionType: '',
  dialogTitle: '',
  showSearch: true,
  total: 1,
  loading: false,
  operType: '',
  detailId: '',
  data: {}
})
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: null,
  type: null
})
const tableList = ref([])

function handleSearch(confirm) {
  !confirm && queryRef.value.resetFields()
  getList(true)
}

async function getList(refresh) {
  state.loading = true
  if (refresh) queryParams.value.pageNum = 1
  try {
    const res = await getAdvertList(queryParams.value)
    tableList.value = res.rows
    state.total = res.total
  } finally {
    state.loading = false
  }
}

/**table Action */
function handleAdd() {
  state.operType = 'add'
  state.data = {}
  proxy.$refs['operateRef'].show('add')
}
function handleEdit(row) {
  state.operType = 'edit'
  proxy.$refs['operateRef'].show('edit', row.adId)
}
function handleDelete(obj) {
  ElMessageBox.confirm('确认要删除' + '"' + obj.name + '"' + '吗?', '系统提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    center: false
  })
    .then(() => {
      deleteAdvert(obj.adId).then(() => {
        getList()
        ElMessage.success('删除成功')
      })
    })
    .catch(() => {})
}
// const dialogImageUrl = ref('')
// const dialogVisible = ref(false)
// function handlePictureCardPreview(url) {
//   dialogImageUrl.value = url
//   dialogVisible.value = true
// }

/**init */
function init() {
  getList()
}

init()
</script>

<style lang="scss" scoped></style>
